package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_template;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_template;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_templateSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_templateService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[EMail模板] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_templateServiceImpl implements IMail_templateService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_templateClientService mail_templateClientService;

    @Override
    public Mail_template get(Integer id) {
        Imail_template clientModel = mail_templateClientService.createModel();
        clientModel.setId(id);
        mail_templateClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_template get(Mail_template mail_template) {
        Mail_template retmail_template = this.get(mail_template.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_template.getClass(), mail_template.getClass(), false);
        copier.copy(retmail_template, mail_template, null);
        return mail_template ;
    }

    @Override
    public void updateBatch(List<Mail_template> mail_templates, int batchSize) {

        //mail_templateClientService.updateBatch(mail_templates,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_templateClientService.removeBatch(mail_templates) ;

    }

    @Override
    public boolean update(Mail_template mail_template) {
        boolean bOk = false ;
        Imail_template clientModel = this.convert2Model(mail_template,null);
        mail_templateClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_template) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Mail_template mail_template) {
        boolean bOk = false ;
        Imail_template clientModel = this.convert2Model(mail_template,null);
        mail_templateClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_template);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_template clientModel = mail_templateClientService.createModel();
        clientModel.setId(id);
        mail_templateClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Mail_template> mail_templates, int batchSize) {

                        //mail_templateClientService.createBatch(mail_templates,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_template> searchDefault(Mail_templateSearchContext context) {
        List<Mail_template> list = new ArrayList<Mail_template>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_template> clientModelList = mail_templateClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_template clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_template>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_template> selectByOdoo_create(Integer id){
        Mail_templateSearchContext context = new Mail_templateSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_template> list = new ArrayList<Mail_template>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_template> clientModelList = mail_templateClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_template clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_template>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_template> selectByOdoo_write(Integer id){
        Mail_templateSearchContext context = new Mail_templateSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_template> list = new ArrayList<Mail_template>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_template> clientModelList = mail_templateClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_template clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_template>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_template convert2Model(Mail_template domain , Imail_template model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_templateClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getUse_default_toDirtyFlag())
            model.setUse_default_to(domain.getUse_default_to());
         if(domain.getSubjectDirtyFlag())
            model.setSubject(domain.getSubject());
         if(domain.getAttachment_idsDirtyFlag())
            model.setAttachment_ids(domain.getAttachment_ids());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getReply_toDirtyFlag())
            model.setReply_to(domain.getReply_to());
         if(domain.getSub_objectDirtyFlag())
            model.setSub_object(domain.getSub_object());
         if(domain.getScheduled_dateDirtyFlag())
            model.setScheduled_date(domain.getScheduled_date());
         if(domain.getModel_idDirtyFlag())
            model.setModel_id(domain.getModel_id());
         if(domain.getMail_server_idDirtyFlag())
            model.setMail_server_id(domain.getMail_server_id());
         if(domain.getReport_templateDirtyFlag())
            model.setReport_template(domain.getReport_template());
         if(domain.getBody_htmlDirtyFlag())
            model.setBody_html(domain.getBody_html());
         if(domain.getRef_ir_act_windowDirtyFlag())
            model.setRef_ir_act_window(domain.getRef_ir_act_window());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getPartner_toDirtyFlag())
            model.setPartner_to(domain.getPartner_to());
         if(domain.getModelDirtyFlag())
            model.setModel(domain.getModel());
         if(domain.getEmail_fromDirtyFlag())
            model.setEmail_from(domain.getEmail_from());
         if(domain.getAuto_deleteDirtyFlag())
            model.setAuto_delete(domain.getAuto_delete());
         if(domain.getEmail_ccDirtyFlag())
            model.setEmail_cc(domain.getEmail_cc());
         if(domain.getReport_nameDirtyFlag())
            model.setReport_name(domain.getReport_name());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getModel_object_fieldDirtyFlag())
            model.setModel_object_field(domain.getModel_object_field());
         if(domain.getEmail_toDirtyFlag())
            model.setEmail_to(domain.getEmail_to());
         if(domain.getNull_valueDirtyFlag())
            model.setNull_value(domain.getNull_value());
         if(domain.getCopyvalueDirtyFlag())
            model.setCopyvalue(domain.getCopyvalue());
         if(domain.getUser_signatureDirtyFlag())
            model.setUser_signature(domain.getUser_signature());
         if(domain.getSub_model_object_fieldDirtyFlag())
            model.setSub_model_object_field(domain.getSub_model_object_field());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getLangDirtyFlag())
            model.setLang(domain.getLang());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Mail_template convert2Domain( Imail_template model ,Mail_template domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_template();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getUse_default_toDirtyFlag())
            domain.setUse_default_to(model.getUse_default_to());
        if(model.getSubjectDirtyFlag())
            domain.setSubject(model.getSubject());
        if(model.getAttachment_idsDirtyFlag())
            domain.setAttachment_ids(model.getAttachment_ids());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getReply_toDirtyFlag())
            domain.setReply_to(model.getReply_to());
        if(model.getSub_objectDirtyFlag())
            domain.setSub_object(model.getSub_object());
        if(model.getScheduled_dateDirtyFlag())
            domain.setScheduled_date(model.getScheduled_date());
        if(model.getModel_idDirtyFlag())
            domain.setModel_id(model.getModel_id());
        if(model.getMail_server_idDirtyFlag())
            domain.setMail_server_id(model.getMail_server_id());
        if(model.getReport_templateDirtyFlag())
            domain.setReport_template(model.getReport_template());
        if(model.getBody_htmlDirtyFlag())
            domain.setBody_html(model.getBody_html());
        if(model.getRef_ir_act_windowDirtyFlag())
            domain.setRef_ir_act_window(model.getRef_ir_act_window());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getPartner_toDirtyFlag())
            domain.setPartner_to(model.getPartner_to());
        if(model.getModelDirtyFlag())
            domain.setModel(model.getModel());
        if(model.getEmail_fromDirtyFlag())
            domain.setEmail_from(model.getEmail_from());
        if(model.getAuto_deleteDirtyFlag())
            domain.setAuto_delete(model.getAuto_delete());
        if(model.getEmail_ccDirtyFlag())
            domain.setEmail_cc(model.getEmail_cc());
        if(model.getReport_nameDirtyFlag())
            domain.setReport_name(model.getReport_name());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getModel_object_fieldDirtyFlag())
            domain.setModel_object_field(model.getModel_object_field());
        if(model.getEmail_toDirtyFlag())
            domain.setEmail_to(model.getEmail_to());
        if(model.getNull_valueDirtyFlag())
            domain.setNull_value(model.getNull_value());
        if(model.getCopyvalueDirtyFlag())
            domain.setCopyvalue(model.getCopyvalue());
        if(model.getUser_signatureDirtyFlag())
            domain.setUser_signature(model.getUser_signature());
        if(model.getSub_model_object_fieldDirtyFlag())
            domain.setSub_model_object_field(model.getSub_model_object_field());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getLangDirtyFlag())
            domain.setLang(model.getLang());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
